package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Common;
import com.example.elderlycare.dao.entity.Complaint;
import com.example.elderlycare.dao.mapper.CommonMapper;
import com.example.elderlycare.dao.mapper.ComplaintMapper;
import com.example.elderlycare.service.ComplaintService;
import com.example.elderlycare.service.entity.ComplaintItem;
import com.example.elderlycare.service.entity.ComplaintListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint>
        implements ComplaintService {
    @Resource
    ComplaintMapper complaintMapper;

    @Resource
    CommonMapper commonMapper;

    @Override
    public ComplaintListResponseBody queryComplaints(Map<String, Object> params) {
        QueryWrapper<Common> queryWrapper = new QueryWrapper<>();
        /* type = 15、16、17、18 分别对应投诉方式、处理状态、回访状态、满意度的字典 */
        queryWrapper.ge("type", 15).le("type", 18);
        List<Common> commonList = commonMapper.selectList(queryWrapper);
        Map<Integer, Common> commonMap = new HashMap<>();
        commonList.forEach((item) -> commonMap.put(item.getId(), item));

        List<Complaint> complaints = complaintMapper.queryComplaints(params);
        int total = complaintMapper.getComplaintsCount(params);
        List<ComplaintItem> complaintItems = new ArrayList<>();

        complaints.forEach((item) -> {
            ComplaintItem complaintItem = ComplaintItem.builder()
                    .id(item.getId())
                    .complainWayId(item.getComplainWayId())
                    .complainWay(item.getComplainWayId() != null ?
                            commonMap.get(item.getComplainWayId()).getName() : null)
                    .elderId(item.getElderId())
                    .elderName(item.getElderName())
                    .acceptor(item.getAcceptor())
                    .accuser(item.getAccuser())
                    .defendant(item.getDefendant())
                    .handleStatusId(item.getHandleStatusId())
                    .handleStatus(item.getHandleStatusId() != null ?
                            commonMap.get(item.getHandleStatusId()).getName() : null)
                    .replySatisfactionId(item.getReplySatisfactionId())
                    .replySatisfaction(item.getReplySatisfactionId() != null ?
                            commonMap.get(item.getReplySatisfactionId()).getName() : null)
                    .replyStatusId(item.getReplyStatusId())
                    .replyStatus(commonMap.get(item.getReplyStatusId()).getName())
                    .time(item.getTime())
                    .build();
            complaintItems.add(complaintItem);
        });
        return ComplaintListResponseBody.builder()
                .total(total)
                .complaintItemList(complaintItems)
                .build();
    }
}
